function G=def_draw_game_init(G)
% def_draw_game_init: Do some initialization work before the simulation loop
%
% SYNTAX:
%   G = def_draw_game_init(G)
%   Initialize the game object G for the simulation loop and return the game object.
%   This routine clears the simulation windows and set up some properties (grid,axis,etc).
%   By default, the main simulation window will be put at top-left corner (staying on top), 
%   and the data plotting window will be put at top-right corner (staying on top).

%   Author: Hongbin Ma
%   Last updated: 2009-6-1 with MATLAB 6.5
%   Problems or suggestions? Email me: mathmhb[at]163.com

if G.draw<1,
  return;
end;
close all;
figure(G.figure);clf;
if ~isempty(G.axis),
  axis manual;
  axis(G.axis);
end;
if ~isempty(G.grid),
  grid(G.grid);
end;
if ~isempty(G.window_size),
  set(gcf,'Position',[G.window_pos,G.window_size]);
end;
if ~isempty(G.ontop),
  winontop(G.figure,G.ontop);
end;

if G.draw>1,
  figure(G.figure2);clf;
  if ~isempty(G.axis2),
    axis manual;
    axis(G.axis2);
  end;
  if ~isempty(G.grid2),
    grid(G.grid2);
  end;
  if ~isempty(G.window_size),
    win_pos2=[G.window_size(1)+G.window_pos(1)+5,G.window_pos(2),G.window_size];
    set(gcf,'Position',win_pos2);
  end;
  if ~isempty(G.ontop),
    winontop(G.figure,G.ontop);
  end;
  set_pos_plots(G);
  figure(G.figure);
end;

function set_pos_plots(G)
m=size(G.plot_components,1);
n=size(G.plot_components,2);
if m~=1|n<=1,
  return;
end;
fig=G.figure2;
wx=G.screen_size(1)/(n-1);
wy=G.window_pos(2)-40;
px=0;
py=wy;
for i2=2:n,
    j=G.plot_components(i2);
    if j<1,continue;end;
    fig=fig+1;figure(fig);
    xx=px+(i2-2)*wx;
    yy=0;
    set(gcf,'Position',[xx,yy,wx,wy]);
end;
